﻿/*
创建人：zhangrj
日期：2022-08-05
描述：工步检验弹窗页面js
*/
var page = {
    url: getRootPath() + "/Ashx/ContainerStepInfoCheckHandler.ashx", //后台处理url,
    //设备分页参数
    parentID: "", //父窗体ID
    init: function () {
        page.bind();
        page.initData();
    },
    //绑定事件
    bind: function () {
        //列表加载
        $("#gdContainerInfo").datagrid({
            columns: [[
                 { field: 'MFGORDERNAME', title: '订单号', width: 120 },
                 { field: 'PRODUCTBATCH', title: '件号/批次号', width: 250 },
                 { field: 'WIPQTY', title: '在制数', width: 50 },
                 { field: 'WORKFLOWSTEPNAME', title: '工序', width: 180 },
                 { field: 'STEPINFONAME', title: '工步', width: 150 },
                 { field: 'CONTAINERTYPENAME', title: '批次类型', width: 100 },
                 { field: 'REPORTQTY', title: '已报数', width: 50 },
                 { field: 'FIRSTCHECKNUM', title: '首检', width: 50, formatter: function (value, row, index) {
                     if (value === 1) {
                         return "是";
                     }
                     else { return "否"; }
                 }
                 },
                 { field: 'STEPCHECKNUM', title: '序检', width: 50, formatter: function (value, row, index) {
                     if (value === 1) {
                         return "是";
                     }
                     else { return "否"; }
                 }
                 }
                ]]
        });

        //检验确认
        $("#btnCheckSure").on("click", function () {
            page.checkInfo();
        });
        //关闭
        $("#btnClose").on("click", function () {
            parent.window.returnValue = { isSuccess: true, message: "已关闭窗体" };
            parent.$("#" + page.parentID).dialog("close");
        });
    },
    //初始化页面数据
    initData: function () {
        page.parentID = getParam("parentDiv");
        var poPupData = parent.$("#" + page.parentID).dialog('options').poPupData;
        if (poPupData.length == 0) {
            displayMessage("缓存过期", false);
            return;
        }
        var containerType = poPupData[0].container[0].CONTAINERTYPE;
        //批次列表
        $("#gdContainerInfo").datagrid('loadData', { rows: poPupData[0].container });
        //绑定报工详细列表
        page.post(page.url, { action: "GetStepInfoReportList", params: poPupData[0].reportIds }, true, function (res) {
            if (!res.Result) {
                displayMessage(res.Message, res.Result);
            }
            //检验列表加载
            $("#gdWorkReportInfo").datagrid({
                onDblClickCell: function (index, field, value) {
                    $(this).datagrid('beginEdit', index);
                    var ed = $(this).datagrid('getEditor', { index: index, field: ['LOSSQTY', 'LOSSREASON', 'REWORKQTY', 'DEFECTQTY'] });
                    $(ed.target).focus();
                    $(this).datagrid('checkRow', index);
                },
                onClickRow: function (index) {
                    $(this).datagrid('acceptChanges');
                },
                columns: [[
                 { field: 'ck', title: '选择', checkbox: true },
                 { field: 'CONTAINERNAME', title: '批次号', width: 180 },
                 { field: 'CHILDCONTAINERNAME', title: '子序号', width: 180, hidden: true },
                 { field: 'QTY', title: '报工数', width: 50 },
                 { field: 'EMPLOYEENAME', title: '报工人', width: 80 },
                 { field: 'CHECKTYPE', title: '检验类型', width: 60, formatter: function (value, row, index) {
                     if (value === 1) {
                         return "首检";
                     }
                     else { return "序检"; }
                 }
                 },
                 { field: 'SELFCHECKQUALIFIEDQTY', title: '自检合格数', width: 80 },
                 { field: 'SELFCHECKOUTOFTOLERANCEQTY', title: '自检超差数', width: 80 },
                 { field: 'SELFCHECKLOSSQTY', title: '自检报废数', width: 80 },
                 { field: 'SPECIMENQTY', title: '试件数', width: 80 },
                 { field: 'SPECIMENLOSSQTY', title: '废票试件', width: 80 },
                 { field: 'CREATEDATE', title: '报工日期', width: 100, formatter: function (value, row, index) {
                     return dayjs(value).format("YYYY-MM-DD");
                 }
                 },
                //{ field: 'FIRSTCHECKCONTAINERNO', title: '首件批次号', width: 120 },
                 {field: 'LOSSQTY', title: '报废数', width: 80, editor: "numberbox" },
                 { field: 'LOSSREASON', title: '报废原因', width: 80, formatter: function (value, row, index) {
                     return row.LOSSREASONNAME;
                 }, editor: { type: 'combobox', options:
                          {
                              valueField: 'LOSSREASONID',
                              textField: 'LOSSREASONNAME',
                              data: res.DBDataSet.dtLossReason,
                              editable: false
                          }
                 }
                 },
                 { field: 'REWORKQTY', title: '返修数', width: 80, editor: "numberbox" },
                 { field: 'DEFECTQTY', title: '超差数', width: 80, editor: "numberbox" },
                 { field: 'CHECKNOTES', title: '备注', width: 100, editor: "textbox" }
                ]]
            });
            $("#gdWorkReportInfo").datagrid('loadData', { rows: res.DBDataSet.dtDetail });
            if (containerType === 3) {
                $("#gdWorkReportInfo").datagrid("showColumn", "CHILDCONTAINERNAME"); //如果是批量子序号，显示子序号列
                $("#gdWorkReportInfo").datagrid("hideColumn", "QTY"); //如果是批量子序号，隐藏报工数量
            }
        });
    },
    //检验
    checkInfo: function () {
        $('#btnCheckSure').linkbutton('disable'); //禁用按钮
        $("#gdWorkReportInfo").datagrid('acceptChanges');
        var container = $("#gdContainerInfo").datagrid('getRows');
        var check = $("#gdWorkReportInfo").datagrid('getRows');
        var isVaild = true;
        for (var i = 0; i < check[0].length; i++) {
            var qty = parseInt(check[i].QTY, 10);
            if (container[0].CONTAINERTYPE === 3) {
                qty = 1;
            }
            var loss = parseInt(check[i].LOSSQTY, 10);
            var rework = parseInt(check[i].REWORKQTY, 10);
            var defect = parseInt(check[i].DEFECTQTY, 10);
            if (qty < loss + rework + defect) {
                isVaild = false;
                displayMessage2("报废+超差+返修大于检测数", false, ["btnCheckSure"]);
                break;
            }
        }
        if (isVaild) {
            var formData = {
                action: "SaveStepInfoCheckInfo",
                dtContainer: JSON.stringify(container),
                dtCheck: JSON.stringify(check)
            };
            page.post(page.url, formData, true, function (res) {
                if (res.Result) {
                    parent.window.returnValue = { isSuccess: true, message: "检验成功" };
                    parent.$("#" + page.parentID).dialog("close");
                }
                displayMessage2(res.Message, res.Result, ["btnCheckSure"]);
            });
        }
    },
    //post数据（路径，参数，是否异步，成功返回方法）
    post: function (url, param, async, callback) {
        $.ajax({
            type: "Post",
            url: url,
            data: param,
            cashe: false,
            async: async,
            dataType: "json",
            beforeSend: function () {
                $.messager.progress({ text: '', msg: '处理中...' });
            },
            success: function (data) {
                $.messager.progress('close');
                callback(data);
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                $.messager.progress('close');
                displayMessage(XMLHttpRequest.responseText, false);
                $("body").find(".easyui-linkbutton").each(function () {
                    $(this).linkbutton('enable');
                });
            }
        });
    }
}
page.init();